from abc import ABC, abstractmethod
import serial
from .config import ARDUINO_CONFIG
from .sensor_handler import SensorHandler

class BaseVehicleController(ABC):
    def __init__(self):
        self.arduino = serial.Serial(
            ARDUINO_CONFIG['port'], 
            ARDUINO_CONFIG['baud_rate'],
            timeout=ARDUINO_CONFIG['timeout']
        )
        self.sensor_handler = SensorHandler(self.arduino)
        self.sensor_handler.start()

    @abstractmethod
    def send_command(self, msg):
        pass

    def get_sensor_data(self):
        return self.sensor_handler.get_latest_data()

    def stop(self):
        """停止控制器并释放资源"""
        self.sensor_handler.stop()
        if self.arduino.is_open:
            self.arduino.close()